/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.portlet.editor.gui.view.settings.panel.portlet;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.genia.toolbox.basics.editor.gui.AbstractGUI;
import com.genia.toolbox.portlet.editor.gui.PortletEditorGUI;
import com.genia.toolbox.portlet.editor.model.portlet.PortletType;
import com.genia.toolbox.portlet.editor.model.portlet.impl.LinkedPortletModel;
import com.genia.toolbox.portlet.editor.model.portlet.impl.PortletModel;

/**
 * Linked settings panel.
 */
@SuppressWarnings("serial")
public class LinkedPortletSettingsPanel
    extends AbstractPortletSettingsPanel
{

  /**
   * The link label.
   */
  private JLabel labelLink = null;

  /**
   * The link combo box.
   */
  private JComboBox comboLink = null;



  /**
   * Constructor.
   * 
   * @param portletEditorGUI
   *          the application portletEditorGUI.
   */
  public LinkedPortletSettingsPanel(PortletEditorGUI portletEditorGUI)
  {
    super(portletEditorGUI);
  }



  /**
   * Initialise the panel.
   */
  @Override
  public void initialise()
  {
    super.initialise();
  }



  /**
   * Create the specific panel.
   * 
   * @return the specific panel.
   */
  @Override
  public JPanel createSpecificPanel()
  {
    JPanel panel = new JPanel();

    GridBagLayout layout = new GridBagLayout();
    GridBagConstraints constraints = new GridBagConstraints();
    panel.setLayout(layout);

    this.labelLink = new JLabel(this.getGui().getController().notifyTranslation("com.genia.toolbox.portlet.editor.gui.panel.portlets.LinkedSettingsPanel.link"));
    constraints.gridx = 1;
    constraints.gridy = 1;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 0;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.insets = new Insets(0, 0, 0, 0);
    panel.add(this.labelLink, constraints);

    List<String> portlets = this.getGui().getController().notifyDisplayPortlets();
    this.comboLink = new JComboBox(portlets.toArray());
    this.comboLink.addActionListener(this);
    constraints.gridx = 2;
    constraints.gridy = 1;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.insets = new Insets(0, 5, 0, 0);
    panel.add(this.comboLink, constraints);

    constraints.gridx = 1;
    constraints.gridy = 3;
    constraints.gridwidth = 2;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 1;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(0, 0, 0, 0);
    panel.add(new JLabel(), constraints);

    return panel;
  }



  /**
   * Set the displayed portlet.
   * 
   * @param model
   *          the portlet model.
   */
  @Override
  public void setPortlet(PortletModel model)
  {
    this.removeAllListeners();
    super.setPortlet(model);
    this.hideIdentifier();
    LinkedPortletModel linkedModel = (LinkedPortletModel) model;
    this.textPortletName.setText(PortletType.Linked.name().toLowerCase());
    this.areaDescription.setText(this.getGui().getController().notifyTranslation("com.genia.toolbox.portlet.editor.gui.dialog.PortletSelectorDialog.area.linked"));
    if (linkedModel.getLink() == null || linkedModel.getLink().trim().length() == 0) {
      this.comboLink.setSelectedItem(null);
    }
    else {
      this.comboLink.setSelectedItem(linkedModel.getElementName());
    }
    this.addAllListeners();
    this.validateSettings();
  }



  /**
   * The displayed portlet model.
   * 
   * @return the displayed portlet model.
   */
  @Override
  public PortletModel getPortlet()
  {
    LinkedPortletModel updatedModel = (LinkedPortletModel) super.getPortlet();
    updatedModel.setLink((String) this.comboLink.getSelectedItem());
    return updatedModel;
  }



  /**
   * Remove all listeners.
   */
  @Override
  public void removeAllListeners()
  {
    super.removeAllListeners();
    this.comboLink.removeActionListener(this);
  }



  /**
   * Add all listeners.
   */
  @Override
  public void addAllListeners()
  {
    super.addAllListeners();
    this.comboLink.addActionListener(this);
  }



  /**
   * Handle the action events.
   * 
   * @param e
   *          the action event.
   */
  @Override
  public void actionPerformed(ActionEvent e)
  {
    super.actionPerformed(e);
    if (e.getSource().equals(this.comboLink)) {
      this.validateSettings();
      this.getGui().notifyElementSettingsChanged(this.getPortlet());
    }
  }



  /**
   * Check whether the settings are valid.
   */
  @Override
  public void validateSettings()
  {
    super.validateSettings();
    if (this.comboLink.getSelectedItem() == null) {
      this.labelLink.setForeground(AbstractGUI.COLOR_TEXT_ERROR);
    }
    else {
      this.labelLink.setForeground(AbstractGUI.COLOR_TEXT);
    }
  }

}
